<html>
<head></head>
<body>
<script type="text/javascript">
    var socket = null;
    var wsuri = "ws://127.0.0.1:1234";
    window.onload = function() {
        console.log("onload");
        socket = new WebSocket(wsuri);
        // 建立连接后触发
        socket.onopen = function() {
            console.log("connected to " + wsuri);
        }
        // 关闭连接时触发
        socket.onclose = function(e) {
            console.log("connection closed (" + e.code + ")");
        }
        // 收到消息后触发
        socket.onmessage = function(e) {
            console.log("message received: " + e.data);
        }
        // 发生错误时触发
        socket.onerror = function(e) {
            console.log("error: socket connection failed")
        }
    };
    function send() {
        var msg = document.getElementById('message').value;
        socket.send(msg);
    };
</script>
<h1>WebSocket Echo Test</h1>
<form>
    <p>
        Message: <input id="message" type="text" value="Hello, world!">
    </p>
</form>
<button onclick="send();">Send Message</button>
</body>
</html>